﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Objects;
using Arbalest.Models;
using System.Web.Configuration;
using Arbalest.Web.Helpers;
using System.Data;
using System.Reflection;

namespace Arbalest.Web.Controllers
{
    [Flags]
    public enum DataStoreMode
    {
        EntityFramework = 0x1,
        MongoDb         = 0x2,
        Custom          = 0x3fff
    }

    public class DataStore
    {
        public DataStoreMode DataStoreMode
        {
            get;
            private set;
        }

        #region constructors
        
        public DataStore()
            : this(DataStoreMode.EntityFramework, string.Empty)
        {
        }

        public DataStore(DataStoreMode mode, string initializationParameters)
        {
            this.DataStoreMode = mode;
        }
        #endregion constructors

        public IQueryable<T> GetList<T>() where T : class
        {
            IQueryable<T> resultList = null;
            
            if (this.DataStoreMode == Controllers.DataStoreMode.EntityFramework)
            {
                resultList = EntityFrameworkHelper.GetList<T>();
            }

            return resultList;

        }

        public void Add<T>(T source) where T : class
        {
            if (this.DataStoreMode == Controllers.DataStoreMode.EntityFramework)
            {
                EntityFrameworkHelper.Add<T>(source);
            }
        }

        public void Remove<T>(T source) where T : class
        {
            if (this.DataStoreMode == Controllers.DataStoreMode.EntityFramework)
            {
                EntityFrameworkHelper.Remove<T>(source);
            }
        }

        public void Update<T>(T source) where T : class
        {
            if (this.DataStoreMode == Controllers.DataStoreMode.EntityFramework)
            {
                EntityFrameworkHelper.Update<T>(source);
            }
        }
    }

    

    public class WarehouseController : Controller
    {
        public ActionResult Index()
        {
            DataStore ds = new DataStore();

            return View(
                ds.GetList<Warehouse>()
                );
        }

        //
        // GET: /Warehouse/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Warehouse/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Warehouse/Create

        [HttpPost]
        public ActionResult Create(Warehouse warehouse, FormCollection collection)
        {
            try
            {
                DataStore ds = new DataStore();
                ds.Add<Warehouse>(warehouse);
                
                TempData[DataHelper.TempData.Alert] = new Alert
                {
                    Context = Alert.AlertContext.Success,
                    Markup = "Record added."
                };

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Warehouse/Edit/5

        public ActionResult Edit(int id)
        {
            DataStore ds = new DataStore();

            var result = ds.GetList<Warehouse>().Where(rec => rec.Id == id);

            return View(
                ds.GetList<Warehouse>().Where(rec => rec.Id == id).FirstOrDefault()
                );
        }

        //
        // POST: /Warehouse/Edit/5

        [HttpPost]
        public ActionResult Edit(Warehouse userInput, FormCollection collection)
        {
            try
            {
                DataStore ds = new DataStore();
                ds.Update<Warehouse>(userInput);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Warehouse/Delete/5
        public ActionResult Delete(int id)
        {
            DataStore ds = new DataStore();

            //var result = from product in products
            //             where product.ProductID == productID
            //             select product;
            var result = ds.GetList<Warehouse>().Where(rec => rec.Id == id);
            ViewBag.sql = ((ObjectQuery<Warehouse>)result).ToTraceString();
//            Console.WriteLine(((ObjectQuery<Product>)result).ToTraceString());

            return View(
                ds.GetList<Warehouse>().Where(rec => rec.Id==id).FirstOrDefault()
                );
            
        }

        //
        // POST: /Warehouse/Delete/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                DataStore ds = new DataStore();

                Warehouse warehouse = ds.GetList<Warehouse>().Where(rec => rec.Id == id).FirstOrDefault();
                ds.Remove<Warehouse>(warehouse);

                TempData[DataHelper.TempData.Alert] = new Alert
                {
                    Context = Alert.AlertContext.Success,
                    Markup = "Record removed."
                };

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
